package com.seabook.store.auth.domain;

import java.util.List;

public class User implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 9079125782518742302L;

    public static final Object TYPE_ADMIN = "ADMIN";

    private int userId;

    private String userName;

    private boolean admin = false;

    private String email;

    private String phone;

    private Address defaultReceiver = new Address();

    private List<Address> receiver;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Address getDefaultReceiver() {
        return defaultReceiver;
    }

    public void setDefaultReceiver(Address defaultReceiver) {
        this.defaultReceiver = defaultReceiver;
    }

    public List<Address> getReceiver() {
        return receiver;
    }

    public void setReceiver(List<Address> receiver) {
        this.receiver = receiver;
        if (null != receiver && receiver.size() != 0) {
            for (Address address : receiver) {
                if (address.isDefaultR()) {
                    this.defaultReceiver = address;
                }
            }
            this.defaultReceiver = receiver.get(0);
        } 
    }

    public Address getDefaultAddress() {
        return this.defaultReceiver;
    }

    public Address findAddress(int addressId) {
        for(Address address: receiver){
            if(address.getId() == addressId){
                return address;
            }
        }
        return new Address();
    }

    public void updateAddress(Address address) {
        Address existingAddress = findAddress(address.getId());
        int index = receiver.lastIndexOf(existingAddress);
        receiver.remove(existingAddress);
        receiver.add(index, address); 
    }

}
